<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<button onclick="load()">start</button>
<script>
    const timeout = 60 * 1000
    let timer
    const urls = []
    let total = 0
    const clip_info_list = []
    function getClip(res) {
        if (/playurl/.test(res.url)) {
            // console.log(res.url,res)
            const lindex = clip_info_list.findIndex(d=>d.url === res.url)
            console.log(lindex)
            if (lindex!==-1) return
            const clip = res.result.video_info.clip_info_list.map((d,i)=> i === 0 ?d.end: d.start)
            clip_info_list.push({
                url: res.url,
                clip
            })
            if (!urls.length) {
                saveDataAsFile('clip.txt',JSON.stringify(clip_info_list))
            }else start()

        }

    }
    function getBiBiliUrl() {
        let list = Array.from(document.querySelectorAll('.eplist_ep_list_wrapper__Sy5N8 .numberList_wrapper___SI4W .numberListItem_number_list_item__T2VKO'))
        list = list.filter(d=> {
            const tag = d.querySelector('.numberListItem_badge__Ex_1n')
            if (tag?.innerHTML === '预告') {
                return false
            }
            return true
        })
        return list.map(d=>d.querySelector('a').href)
    }

    // 配合m3u8 浏览器插件
    window.addEventListener("message", (event) => {
        const data = event.data;
        if (data) {
            const type = data.type;
            // console.log(data.type,data)
            if (type ==='http') {
                try {
                    const url = data.params.response.url
                    if (!data.response.base64Encoded) {
                        getClip({url,...JSON.parse(data.response.body)})
                    }

                }catch (e) {

                }

            }else if (['m3u8-data','CONTENT_EVENT'].includes(type)) {
                const iframe = document.querySelectorAll(`iframe.m3u8-getter`);
                document.body.removeChild(iframe[0]);
                setTimeout(start,3000)
            }
        }
    })

    function start() {
        const iframeOld = document.querySelectorAll(`iframe.m3u8-getter`);
        const retry = this.retry
        if (timer) {
            clearTimeout(timer)
            timer = null
        }
        if (iframeOld?.length) {
            const c = iframeOld[0]
            document.body.removeChild(c);
        }
        if (!urls.length) return alert('已完成');
        const url = retry?iframeOld?.[0].src : urls.shift();
        // url.click()
        const iframe = document.createElement('iframe');
        iframe.classList.add('m3u8-getter')
        // iframe.style.cssText = 'position: fixed;left:0;top:0;width:100%;height:100%'
        document.body.appendChild(iframe);
        iframe.src = url;
        timer = setTimeout(()=>start(1),timeout)
    }

    function load() {
        for (let i = 117; i <= 131; i++) {
            if (i ==125 || i==126) continue;
            urls.push(`https://www.dalvdm.cc/index.php/vod/play/id/233/sid/4/nid/${i}.html`)
            // urls.push(`https://www.chinatownfilm.com/vodplay/843-10-${i}.html`)
        }
        // const links = getBiBiliUrl()
        // total  = links.length
        // urls.push(...links)
        start()
    }

    load()

    // 把数据保存成文件
    const saveDataAsFile = (fileName, data) => {
        let urlObject = window.URL || window.webkitURL || window
        const blobType = '[object Blob]'
        const type = Object.prototype.toString.call(data)
        let downloadData = blobType === type ? data : new Blob([data])

        let save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a')
        save_link.href = urlObject.createObjectURL(downloadData)
        save_link.download = fileName
        let ev = document.createEvent('MouseEvents')
        ev.initMouseEvent(
            'click',
            true,
            false,
            window,
            0,
            0,
            0,
            0,
            0,
            false,
            false,
            false,
            false,
            0,
            null,
        )
        save_link.dispatchEvent(ev)
    }

    const s = 'https://c1.7bbffvip.com/video/wuni/%E7%AC%AC'
    const s1 = '%E9%9B%86/index.m3u8'
    let ss = ''
    const name = 'wuli'
    for (let i = 1; i <= 25; i++) {
        ss += s + (i < 10 ? `0${i}` : i) + s1 + ` ${name}${i}\n`
    }
    saveDataAsFile('test.txt', ss)
</script>
</body>
</html>
